from PyQt6.QtWidgets import QVBoxLayout, QHBoxLayout, QSizePolicy
from PyQt6.QtGui import QFont
from PyQt6.QtCore import Qt
from qfluentwidgets import CardWidget, BodyLabel, StrongBodyLabel, CaptionLabel, ElevatedCardWidget


class InfoCard(ElevatedCardWidget):
    def __init__(self, title, value="0", unit="", icon="📊", color="#243243"):
        super().__init__()

        self.icon = icon
        self.color = color

        # 设置固定大小和样式策略
        self.setFixedSize(250, 160)
        self.setSizePolicy(QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Fixed)

        # 主布局
        main_layout = QVBoxLayout(self)
        main_layout.setContentsMargins(20, 16, 20, 16)
        main_layout.setSpacing(5)

        # 顶部：图标和标题
        top_layout = QVBoxLayout()
        top_layout.setSpacing(10)


        # 图标标签
        icon_label = BodyLabel(icon, self)
        icon_label.setFont(QFont("Segoe UI Emoji", 20))
        icon_label.setFixedSize(35, 35)
        icon_label.setAlignment(Qt.AlignmentFlag.AlignCenter)

        # 标题标签
        title_label = CaptionLabel(title)
        title_label.setFont(QFont("Microsoft YaHei", 18))
        title_label.setStyleSheet("color: #666666;")
        title_label.setAlignment(Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)

        top_layout.addWidget(icon_label)
        top_layout.addWidget(title_label)
        top_layout.addStretch()

        # 数值标签
        self.value_label = StrongBodyLabel(f"{value} {unit}")
        self.value_label.setFont(QFont("Microsoft YaHei", 20, QFont.Weight.Bold))
        self.value_label.setStyleSheet(f"color: {color};")
        self.value_label.setAlignment(Qt.AlignmentFlag.AlignLeft)

        # 添加到主布局
        main_layout.addLayout(top_layout)
        main_layout.addWidget(self.value_label)
        main_layout.addStretch()

    def update_value(self, value, unit=""):
        """更新数值"""
        self.value_label.setText(f"{value} {unit}")
        self.update_color(self.color)

    def update_color(self, color):
        """更新颜色主题"""
        self.color = color
        self.value_label.setStyleSheet(f"color: {color}; margin-top: 6px;")